layui.use(['form', 'table','layuimini','element'], function () {
    var $ = layui.jquery,
        form = layui.form,
        table = layui.table,
        layuimini = layui.layuimini;

    /**
     * 初始化表单，要加上，不然刷新部分组件可能会不加载
     */
    form.render();

    var BoPhotoAlbum = {
        id: 'BoPhotoAlbumTable'
    };

    // 当前弹出层，防止ID被覆盖
    var parentIndex = layer.index;

    var column = [
        [
            {type: 'radio'},
            {field: 'name', title: '相册名称', align: 'center'},
            {field: 'type', title: '类型', align: 'center',
                templet: function (object) {
                    if(object.type == 0){
                        return '<span class="layui-badge layui-bg-green">照片</span>';
                    }else{
                        return '<span class="layui-badge layui-bg-cyan">视频</span>';
                    }
                }
            },
            {field: 'content', title: '简介', align: 'center',
                templet: function (object) {
                    if(object.content == '' || object.content == null){
                        return '-';
                    }else{
                        return object.content;
                    }
                }
            },
            {field: 'isLock', title: '是否加锁', align: 'center', width: 100,
                templet: function (object) {
                    if(object.isLock == 0){
                        return '<input type="checkbox" lay-filter="l_isLockSwitch" lay-skin="switch" lay-text="是|否" disabled>';
                    }else{
                        return '<input type="checkbox" lay-filter="l_isLockSwitch" lay-skin="switch" lay-text="是|否" checked disabled>';
                    }
                }
            },
            {field: 'password', title: '密码', align: 'center',
                templet: function (object) {
                    if(object.password == '' || object.password == null){
                        return '-';
                    }else{
                        return object.password;
                    }
                }
            },
            {field: 'isShow', title: '是否显示', align: 'center', width: 100,
                templet: function (object) {
                    if(object.isShow == 0){
                        return '<input type="checkbox" lay-filter="l_isShowSwitch" lay-skin="switch" lay-text="是|否" checked disabled>';
                    }else{
                        return '<input type="checkbox" lay-filter="l_isShowSwitch" lay-skin="switch" lay-text="是|否" disabled>';
                    }
                }
            },
            {field: 'readNum', title: '阅读数量', align: 'center', width: 100},
            {field: 'praiseNum', title: '赞数量', align: 'center', width: 100},
            {field: 'img', title: '相册封面图', align: 'center',
                templet: function (object) {
                    var img = '<img src="/photo_album_img/' + object.img + '"';
                    return img;
                }
            },
            {field: 'updateDate', title: '更新时间', minWidth: 120, align: 'center'},
            {field: 'createDate', title: '创建时间', minWidth: 120, align: 'center'}
        ]
    ];

    table.render({
        elem: '#' + BoPhotoAlbum.id,
        url: '/web/boPhotoAlbum/list?type=' + $("#in_type").val(),
        toolbar: '#in_toolbarBtn',
        defaultToolbar: [],
        cols: column,
        limits: [10, 15, 20, 25, 50, 100],
        limit: 15,
        page: true
    });

    // 监听搜索操作
    $("#in_data-search-btn").on('click', function () {

        //获取搜索条件
        var name = $("#in_search_name").val();

        //封装
        var dataObject = {};
        dataObject['name'] = name;
        var result = JSON.stringify(dataObject);

        //执行搜索重载
        table.reload(BoPhotoAlbum.id, {
            page: {
                curr: 1
            }
            , where: {
                searchParams: result
            }
        }, 'data');

        return false;
    });

    // 监听重置操作
    $("#in_data-reset-btn").on('click', function () {

        //初始化
        $("#in_search_name").val("");

        //封装
        var dataObject = {};
        dataObject['name'] = "";
        var result = JSON.stringify(dataObject);

        //执行搜索重载
        table.reload(BoPhotoAlbum.id, {
            page: {
                curr: 1
            }
            , where: {
                searchParams: result
            }
        }, 'data');

        return false;
    });


    // 监听确定选择操作
    $(".layuimini-container").on("click", "#in_choose-btn", function () {
        var photoAlbumId = $("#in_photoAlbumId").val();
        var photoAlbumName = $("#in_photoAlbumName").val();
        $("#photoAlbumId").val(photoAlbumId);
        $("#photoAlbumName").val(photoAlbumName);
        if($("#photoAlbumId").val() != '' && $("#photoAlbumId").val() != null && $("#photoAlbumId").val() != undefined){
            $("#uploadPhotoBtn").removeClass("layui-btn-disabled"); //选择相册时则去掉多照片上传按钮disabled
            $("#uploadPhotoBtn").removeAttr("disabled");
        }
        // 关闭弹出层
        layer.close(parentIndex);
        return true;
    });


    //监听表格单选按钮
    table.on('radio(BoPhotoAlbumTable)', function (obj) {
        $("#in_photoAlbumId").val(obj.data.id);
        $("#in_photoAlbumName").val(obj.data.name);
    });

});